package com.jugheadzhou.core.message.rabbitmq.producer.config;

import com.jugheadzhou.core.message.rabbitmq.producer.RabbitSender;
import com.jugheadzhou.core.message.rabbitmq.producer.callback.RabbitDefaultConfirmCallBack;
import com.jugheadzhou.core.message.rabbitmq.producer.callback.RabbitDefaultReturnCallback;
import com.jugheadzhou.core.utils.JsonUtil;
import lombok.AllArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置rabbit的发送模板类的属性
 *
 * @author jugheadzhou
 * @date 2021-12-03
 */
@Configuration
@AllArgsConstructor
public class RabbitProducerConfig implements InitializingBean {

    private RabbitTemplate rabbitTemplate;

    @Bean
    public RabbitSender rabbitSender(){
        return new RabbitSender(rabbitTemplate);
    }

    @Override
    public void afterPropertiesSet() {
        // 自定义objectMapper 主要针对java8的时间API进行兼容
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter(JsonUtil.getObjectMapper()));
        // 配置rabbit发送回调
        rabbitTemplate.setConfirmCallback(new RabbitDefaultConfirmCallBack());
        rabbitTemplate.setReturnsCallback(new RabbitDefaultReturnCallback());
    }

}
